package Test.credit;

import Test.assertCompany.AssertCompanyTest;
import com.itech.ups.app.borrowerCredit.dataUtils.factory.CallFactory;
import com.itech.ups.app.borrowerCredit.dataUtils.factory.XlxxFactory;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

public class PdfTest {
    private static final Logger logger = LoggerFactory.getLogger(AssertCompanyTest.class);

    /**
     * 生成hello world
     *
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    @Test
    public void test1() throws Exception {

        // 1.新建document对象
        Document document = new Document();

        // 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/pdf/test.pdf"));

        // 3.打开文档
        document.open();

        // 4.添加一个内容段落
        document.add(new Paragraph("Hello World!"));

        // 5.关闭文档
        document.close();
    }

    @Test
    public void test2() throws DocumentException, FileNotFoundException, com.itextpdf.text.DocumentException {
        //创建文件
        Document document = new Document();
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/pdf/test5.pdf"));
        //打开文件
        document.open();
        //添加内容
        document.add(new Paragraph("HD content here"));

        //添加有序列表
        List orderedList = new List(List.ORDERED);
        orderedList.add(new ListItem("Item one"));
        orderedList.add(new ListItem("Item two"));
        orderedList.add(new ListItem("Item three"));
        document.add(orderedList);

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();
    }

    /**
     * 生成表格
     */
    @Test
    public void test3() throws Exception {
        //创建文件
        Document document = new Document();
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/pdf/test4.pdf"));
        //打开文件
        document.open();
        //添加内容
        document.add(new Paragraph("HD content here"));

        // 3列的表.
        PdfPTable table = new PdfPTable(3);
        // 宽度100%填充
        table.setWidthPercentage(100);
        // 前间距
        table.setSpacingBefore(10f);
        // 后间距
        table.setSpacingAfter(10f);

        java.util.List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f, 2f, 3f};
        table.setWidths(columnWidths);

        //行1
        PdfPCell cells1[] = new PdfPCell[3];
        PdfPRow row1 = new PdfPRow(cells1);

        //单元格
        cells1[0] = new PdfPCell(new Paragraph("HD content here"));

        //边框验证
        cells1[0].setBorderColor(BaseColor.BLUE);
        //左填充20
        cells1[0].setPaddingLeft(20);
        //水平居中
        cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

        cells1[1] = new PdfPCell(new Paragraph("222"));
        cells1[2] = new PdfPCell(new Paragraph("333"));

        //行2
        PdfPCell cells2[] = new PdfPCell[3];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Paragraph("444"));

        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        //把表格添加到文件中
        document.add(table);

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();
    }

    /**
     * 生成表格  用户身份信息
     * 单个测试
     */
    @Test
    public void test4() throws Exception {
        //创建文件
        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 12, Font.NORMAL);
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/pdf/myPdf.pdf"));
        //打开文件
        document.open();
        //添加内容
        document.add(new Paragraph("用户身份信息", font));

        // 4列的表.
        PdfPTable table = new PdfPTable(4);
        // 宽度100%填充
        table.setWidthPercentage(100);
        // 前间距
         table.setSpacingBefore(20f);
        // 后间距
         table.setSpacingAfter(10f);

        java.util.List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f, 1f, 1f, 1f};
        table.setWidths(columnWidths);

        // 行1
        PdfPCell cells1[] = new PdfPCell[4];
        PdfPRow row1 = new PdfPRow(cells1);
        // 单元格
        cells1[0] = new PdfPCell(new Paragraph("姓名",font));

        // 边框验证
        // cells1[0].setBorderColor(BaseColor.BLUE);
        // 左填充20
        // cells_1[0].setPaddingLeft(20);

        // 水平居中
//        cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
        // 垂直居中
//        cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

        cells1[1] = new PdfPCell(new Paragraph("张三", font));
//        cells1[1].setHorizontalAlignment(Element.ALIGN_CENTER);
//        cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);

        cells1[2] = new PdfPCell(new Paragraph("性别", font));
//        cells1[2].setHorizontalAlignment(Element.ALIGN_CENTER);
//        cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

        cells1[3] = new PdfPCell(new Paragraph("男", font));
//        cells1[3].setHorizontalAlignment(Element.ALIGN_CENTER);
//        cells1[3].setVerticalAlignment(Element.ALIGN_MIDDLE);

        // 行2
        PdfPCell cells2[] = new PdfPCell[4];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Paragraph("年龄",font));
        cells2[1] = new PdfPCell(new Paragraph("35",font));
        cells2[2] = new PdfPCell(new Paragraph("身份证号码",font));
        cells2[3] = new PdfPCell(new Paragraph("123456199001011233",font));

        PdfPCell cells3[] = new PdfPCell[4];
        PdfPRow row3 = new PdfPRow(cells3);
        cells3[0] = new PdfPCell(new Paragraph("身份证归属地",font));
        cells3[1] = new PdfPCell(new Paragraph("广东省白县",font));
        cells3[2] = new PdfPCell(new Paragraph("身份验证结果",font));
        cells3[3] = new PdfPCell(new Paragraph("验证成功",font));

        PdfPCell cells4[] = new PdfPCell[4];
        PdfPRow row4 = new PdfPRow(cells4);
        cells4[0] = new PdfPCell(new Paragraph("四要素验证",font));
        cells4[1] = new PdfPCell(new Paragraph("验证成功",font));
        cells4[2] = new PdfPCell(new Paragraph("公共风险",font));
        cells4[3] = new PdfPCell(new Paragraph("吸毒+在逃",font));


        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        listRow.add(row3);
        listRow.add(row4);
        //把表格添加到文件中
        document.add(table);
        // 追加内容
        yhbin(document);

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();
    }

    /**
     * 测试追加 银行bin
     * @throws Exception
     */
    public void yhbin(Document document) throws Exception {
        BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 12, Font.NORMAL);
        document.add(new Paragraph("银行卡信息", font));

        // 4列的表.
        PdfPTable table = new PdfPTable(4);
        // 宽度100%填充
        table.setWidthPercentage(100);
        // 前间距
        table.setSpacingBefore(5f);
        // 后间距
        table.setSpacingAfter(10f);

        java.util.List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f, 1f, 1f, 1f};
        table.setWidths(columnWidths);

        // 行1
        PdfPCell cells1[] = new PdfPCell[4];
        PdfPRow row1 = new PdfPRow(cells1);
        // 单元格
        cells1[0] = new PdfPCell(new Paragraph("卡号",font));
        cells1[1] = new PdfPCell(new Paragraph("发卡行", font));
        cells1[2] = new PdfPCell(new Paragraph("卡品牌", font));
        cells1[3] = new PdfPCell(new Paragraph("卡类型", font));

        // 行2
        PdfPCell cells2[] = new PdfPCell[4];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Paragraph("5555666677778888",font));
        cells2[1] = new PdfPCell(new Paragraph("招商银行",font));
        cells2[2] = new PdfPCell(new Paragraph("",font));
        cells2[3] = new PdfPCell(new Paragraph("信用卡",font));

        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        //把表格添加到文件中
        document.add(table);
    }

    /**
     * 身份信息表格生成
     * @throws Exception
     */
    public void sfxx(Document document) throws Exception {
        BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 12, Font.NORMAL);

        //打开文件
        document.open();
        //添加内容
        document.add(new Paragraph("用户身份信息", font));

        // 4列的表.
        PdfPTable table = new PdfPTable(4);
        // 宽度100%填充
        table.setWidthPercentage(100);
        // 前间距
        table.setSpacingBefore(5f);
        // 后间距
        table.setSpacingAfter(10f);

        java.util.List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f, 1f, 1f, 1f};
        table.setWidths(columnWidths);

        // 行1
        PdfPCell cells1[] = new PdfPCell[4];
        PdfPRow row1 = new PdfPRow(cells1);
        // 单元格
        cells1[0] = new PdfPCell(new Paragraph("姓名",font));
        cells1[1] = new PdfPCell(new Paragraph("张三", font));
        cells1[2] = new PdfPCell(new Paragraph("性别", font));
        cells1[3] = new PdfPCell(new Paragraph("男", font));

        // 行2
        PdfPCell cells2[] = new PdfPCell[4];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Paragraph("年龄",font));
        cells2[1] = new PdfPCell(new Paragraph("35",font));
        cells2[2] = new PdfPCell(new Paragraph("身份证号码",font));
        cells2[3] = new PdfPCell(new Paragraph("123456199001011233",font));

        PdfPCell cells3[] = new PdfPCell[4];
        PdfPRow row3 = new PdfPRow(cells3);
        cells3[0] = new PdfPCell(new Paragraph("身份证归属地",font));
        cells3[1] = new PdfPCell(new Paragraph("广东省白县",font));
        cells3[2] = new PdfPCell(new Paragraph("身份验证结果",font));
        cells3[3] = new PdfPCell(new Paragraph("验证成功",font));

        PdfPCell cells4[] = new PdfPCell[4];
        PdfPRow row4 = new PdfPRow(cells4);
        cells4[0] = new PdfPCell(new Paragraph("四要素验证",font));
        cells4[1] = new PdfPCell(new Paragraph("验证成功",font));
        cells4[2] = new PdfPCell(new Paragraph("公共风险",font));
        cells4[3] = new PdfPCell(new Paragraph("吸毒+在逃",font));

        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        listRow.add(row3);
        listRow.add(row4);
        //把表格添加到文件中
        document.add(table);


    }
    /**
     * 学历信息表格生成
     * @throws Exception
     */
    public void xlxx(Document document) throws Exception {
        BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 12, Font.NORMAL);
        // {"code":"0000","msg":"处理成功","requestNo":"1527496121629761985",
        // "data":{"major":"国际经济与贸易","graduationDate":"2016","graduation":"毕业","schoolType":"非985及211院校","enrollmentDate":"2012","degreesType":"普通","schoolName":"浙江外国语学院","degrees":"本科"},
        // "timestamp":1527496130431}

        //打开文件
        //document.open();
        //添加内容
        document.add(new Paragraph("用户学历信息", font));

        // 4列的表.
        PdfPTable table = new PdfPTable(4);
        // 宽度100%填充
        table.setWidthPercentage(100);
        // 前间距
        table.setSpacingBefore(5f);
        // 后间距
        table.setSpacingAfter(10f);

        java.util.List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f, 1f, 1f, 1f};
        table.setWidths(columnWidths);

        // 行1
        PdfPCell cells1[] = new PdfPCell[4];
        PdfPRow row1 = new PdfPRow(cells1);
        // 单元格
        cells1[0] = new PdfPCell(new Paragraph("学历",font));
        cells1[1] = new PdfPCell(new Paragraph("毕业院校", font));
        cells1[2] = new PdfPCell(new Paragraph("毕业院校类型", font));
        cells1[3] = new PdfPCell(new Paragraph("专业", font));

        // 行2
        PdfPCell cells2[] = new PdfPCell[4];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Paragraph("本科",font));
        cells2[1] = new PdfPCell(new Paragraph("浙江外国语学院",font));
        cells2[2] = new PdfPCell(new Paragraph("非985及211院校",font));
        cells2[3] = new PdfPCell(new Paragraph("国际经济与贸易",font));

        PdfPCell cells3[] = new PdfPCell[4];
        PdfPRow row3 = new PdfPRow(cells3);
        cells3[0] = new PdfPCell(new Paragraph("入学日期",font));
        cells3[1] = new PdfPCell(new Paragraph("毕业时间",font));
        cells3[2] = new PdfPCell(new Paragraph("学历类型",font));
        cells3[3] = new PdfPCell(new Paragraph("毕业结论",font));

        PdfPCell cells4[] = new PdfPCell[4];
        PdfPRow row4 = new PdfPRow(cells4);
        cells4[0] = new PdfPCell(new Paragraph("2012",font));
        cells4[1] = new PdfPCell(new Paragraph("2016",font));
        cells4[2] = new PdfPCell(new Paragraph("普通",font));
        cells4[3] = new PdfPCell(new Paragraph("毕业",font));

        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        listRow.add(row3);
        listRow.add(row4);
        //把表格添加到文件中
        document.add(table);


    }

    @Test
    public void mainTest() throws Exception {
        // 主方法 创建pdf文件 关闭pdf文件
        //创建文件
        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese, 12, Font.NORMAL);
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/pdf/myPdf.pdf"));

        // 调用各自的追加方法
        // 个人信息
        sfxx(document);
        CallFactory f1 = new XlxxFactory();
        //f1.makePdfTable(document,us);
        yhbin(document);
        xlxx(document);
        // 银行卡bin信息

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();

    }
    @Test
    public void maptest(){
        Map<String, String> map = new HashMap<>();
        System.out.println(map.get("aaa") == null);
    }
}
